#!/bin/sh

set -x
set -e

CODE_HOME=/mnt/code
SOURCE_HOME=/mnt/code/dataduoduo
DEPLOY_HOME=/mnt/resin/webapps

cd $SOURCE_HOME
echo "分支如下:"
git branch -a 
read -p "输入你想检出同步的分支名:"
git checkout $REPLY && git pull


echo "编译代码..."
echo "首先编译依赖包..."
cd /mnt/code/dataduoduo/d3
mvn -Dmaven.test.skip=true clean package install -P test -X
echo -n "输入要编译的项目名:"
read app_name
#cd $SOURCE_HOME/$app_name
#mvn -Dmaven.test.skip=true clean package -P test -X


var=${app_name#*.}

echo "备份历史版本..."
if [ ! -f "$CODE_HOME/backup/$var.war" ]; then
  echo "file not exist"
else
  rm $CODE_HOME/backup/$var.war
fi

if [ ! -f "$DEPLOY_HOME/$var.war" ]; then
  echo "file not exist"
else
  cp -u $DEPLOY_HOME/$var.war $CODE_HOME/backup
fi


echo "停止resin..."
/etc/init.d/resin stop


echo "删除历史版本..."
if [ ! -f "$DEPLOY_HOME/$var.war" ]; then
  echo "file not exist"
else
  rm $DEPLOY_HOME/$var.war
fi

if [ ! -f "$DEPLOY_HOME/$VAR" ]; then
  echo "file not exist"
else
  rm -rf $DEPLOY_HOME/$VAR
fi


echo "移动执行文件..."
cp -u $SOURCE_HOME/$app_name/target/$var.war $DEPLOY_HOME


echo "启动resin..."
/etc/init.d/resin start

echo "查看日志:"
tail -f /mnt/resin/log/$var.log
